﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using Oracle.DataAccess.Client;
using System.IO;
using System.Text;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class AdminDocumentController : BaseController
    {
        //
        // GET: /AdminDocument/

        private IUser_X user;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user).Dispose();
        }

        [InjectionConstructor]
        public AdminDocumentController(IUser_X user)
        {
            this.user = user;
        }

        [Authorize]
        public ActionResult Admin()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            return View();
        }

        [HttpPost]
        public ActionResult Admin(string name, string keywords)
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            DocumentHelper.InsertFile(Request.Files[0], name, User.Identity.Name, keywords);
            return View();
        }

        public ActionResult Search()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin)
                ViewBag.IsConfigAdmin = false;
            var model = DocumentHelper.GetList().OrderByDescending(o => o.CreateTime).ToList();

            return View(model);
        }

        public ActionResult SearchResult(string keywords)
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin)
                ViewBag.IsConfigAdmin = false;

            var model = DocumentHelper.GetListByKeywords(keywords);
            return View(model);
        }

        public ActionResult DownLoad(int id)
        {
            byte[] files;
            string type, filename;
            DocumentHelper.GetFile(id, out files, out type, out filename);
            return File(files, type, filename);
        }

        public ActionResult ListAdmin()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            return PartialView(DocumentHelper.GetList().OrderByDescending(o => o.CreateTime).ToList());
        }

        public ActionResult List()
        {
            var model = DocumentHelper.GetList().OrderByDescending(o => o.CreateTime).Take(5).ToList();
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            DocumentHelper.DeleteFile(id);
            return new EmptyResult();
        }

    }
}
